package com.alvin.demo.utils;

import com.alvin.demo.entity.JobBean;
import org.quartz.*;

public class JobUtils {

    public static void createJob(Scheduler scheduler , JobBean jobBean){
        Class<? extends Job> jobClass = null ;
        JobDetail jobDetail = null ;
        Trigger trigger = null ;
        String cron = jobBean.getCronExpression();
        try{
            jobClass = (Class<? extends Job>) Class.forName(jobBean.getJobClass());
            jobDetail = JobBuilder.newJob(jobClass)
                    .storeDurably() // 持久化
                    .withIdentity(jobBean.getJobName())
                    .usingJobData("count" , 1)
                    .build();

            trigger = TriggerBuilder.newTrigger()
                    .withIdentity(jobBean.getJobName() + "_trigger" )
                    .forJob(jobDetail)
                    .withSchedule(
                            CronScheduleBuilder.cronSchedule(
                                    cron
                            ))
                    .build();
            scheduler.scheduleJob(jobDetail ,trigger) ;
        }catch (ClassNotFoundException | SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void pauseJob(Scheduler scheduler , String jobName){
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    // 恢复任务执行
    public static void resumeJob(Scheduler scheduler , String jobName){
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void deleteJob(Scheduler scheduler , String jobName){
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    // 暂停后执行一次任务
    public static void runOnceJob(Scheduler scheduler , String jobName){
        JobKey jobKey = JobKey.jobKey(jobName);
        try {
            scheduler.triggerJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    public static void modifyJob(Scheduler scheduler , JobBean jobBean){
        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobName() + "_trigger");
        try {
            CronTrigger oldTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);

            String newCron = jobBean.getCronExpression() ;

            CronTrigger trigger = oldTrigger.getTriggerBuilder()
                    .withSchedule(CronScheduleBuilder.cronSchedule(newCron))
                    .build();

            scheduler.rescheduleJob(triggerKey , trigger);

        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
}
